package org.hank.wanandroid.adapter

import android.content.Intent
import android.os.Bundle
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import kotlinx.android.synthetic.main.itemview_square.view.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import org.hank.wanandroid.R
import org.hank.wanandroid.bean.NaviBean
import org.hank.wanandroid.bean.ProjectBean
import org.hank.wanandroid.constants.Constants
import org.hank.wanandroid.http.HttpConfig
import org.hank.wanandroid.ui.TreeDetailsActivity
import org.hank.wanandroid.ui.WebActivity
import org.hank.wanandroid.utils.ArticleHistoryUtil

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 *@auth: Hank
 *邮箱: cs16xiaoc1@163.com
 *创建时间: 2021/5/27 16:45
 *描述: 广场 导航 适配器
 */
class SquareAdapter<T : Any>(data: MutableList<T>) :
    BaseQuickAdapter<T, BaseViewHolder>(R.layout.itemview_square, data) {
    var scope = MainScope()

    override fun convert(holder: BaseViewHolder, item: T) {
        if (item is ProjectBean) {
            holder.itemView.text_itemview_square.text = item.name
            holder.itemView.labelsView_itemview_square.apply {
                var lablelsList: MutableList<String> = ArrayList()
                for (item1 in item.children) {
                    lablelsList.add(item1.name)
                }
                setLabels(lablelsList)
                setOnLabelClickListener { label, data, position ->
                    var intent = Intent(context, TreeDetailsActivity::class.java)
                    var bundle = Bundle()
                    bundle.putString(Constants.TREE_TITLE, item.children[position].name)
                    bundle.putInt(Constants.TREE_ID, item.children[position].id)
                    intent.putExtras(bundle)
                    context.startActivity(intent)
                }
            }
        } else if (item is NaviBean) {
            holder.itemView.text_itemview_square.text = item.name
            holder.itemView.labelsView_itemview_square.apply {
                var lablelsList: MutableList<String> = ArrayList()
                for (item1 in item.articles) {
                    lablelsList.add(item1.title)
                }
                setLabels(lablelsList)
                setOnLabelClickListener { label, data, position ->
                    scope.launch(Dispatchers.IO) {
                        ArticleHistoryUtil.saveArticle(item.articles[position])
                    }
                    var intent = Intent(context, WebActivity::class.java)
                    var bundle = Bundle()
                    var link =
                        if (item.articles[position].link.startsWith("/"))
                            HttpConfig.BASE_URL_MAP + item.articles[position].link
                        else
                            item.articles[position].link
                    bundle.putString(Constants.WEB_URL, link)
                    bundle.putString(Constants.WEB_TITLE, item.articles[position].title)
                    intent.putExtras(bundle)
                    context.startActivity(intent)
                }
            }
        }
    }
}